using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace BardsTaleViewer {
  public class AnimPlayer {
    private IAnim _anim;
    private IAnimFrame _currentFrame;
    private Bitmap _bitmap;
    private Graphics _g;

    private int _delay;
    private int _frameIndex;

    public AnimPlayer (IAnim anim, Bitmap bitmap) {
      _anim = anim;
      _bitmap = bitmap;
      _g = Graphics.FromImage (_bitmap);
      SetFrame (0);
    }

    public void Tick () {
      _delay--;
      if (_delay < 0) {
        if (_anim.DelayBefore) {
          if (_currentFrame.BitmapDiff != null)
            _g.DrawImage (_currentFrame.BitmapDiff, _anim.PosXY.X, _anim.PosXY.Y);
          //if ((_currentFrame is AnimFrame_PC_Bard4) && (_currentFrame.AnimIndex == 2))
          //  Console.WriteLine ("drawing {0}", ((AnimFrame_PC_Bard4) _currentFrame)._pictureIndex2);
        }

        _frameIndex++;
        if (_frameIndex >= _anim.Frames.Count)
          _frameIndex = 0;
        SetFrame (_frameIndex);
      }
    }

    public void SetFrame (int frameIndex) {
      _frameIndex = frameIndex;
      _currentFrame = GetAnimFrame (_frameIndex);
      _delay = _currentFrame.Delay;

      if (!_anim.DelayBefore) {
        if (_currentFrame.BitmapDiff != null)
          _g.DrawImage (_currentFrame.BitmapDiff, _anim.PosXY.X, _anim.PosXY.Y);
      }
    }

    public IAnimFrame GetAnimFrame (int frameIndex) {
      return (_anim.Frames [_frameIndex]);
    }
  }
}
